package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ContextThemeWrapper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.data.GetCommunicateLogData;
import com.example.yiwei.crm.entity.CommunicateLogEntity;
import com.example.yiwei.crm.util.NetWorkUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class DetailedCommunicateLogMessage extends AppCompatActivity {
CommunicateLogEntity communicate;
    private TextView txt_theme,txt_content,txt_next_communicate_date,txt_next_communicate_content,txt_create_name,
            txt_create_time;
    private ImageView img_back,img_delete,img_update;
    int communicate_id;
    AlertDialog.Builder builder;
    public static DetailedCommunicateLogMessage a;
    List<CommunicateLogEntity> list;
    String theme,content,next_content,next_date,creater,create_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_communicate_log_message);
        txt_theme= (TextView) findViewById(R.id.communicate_theme);
        txt_content= (TextView) findViewById(R.id.communicate_cotent);
        txt_next_communicate_date= (TextView) findViewById(R.id.next_communicate_date);
        txt_next_communicate_content= (TextView) findViewById(R.id.next_communicate_cotent);
        txt_create_time= (TextView) findViewById(R.id.communicate_log_create_time);
        txt_create_name= (TextView) findViewById(R.id.communicate_log_create_name);
        img_back= (ImageView) findViewById(R.id.detailed_communicate_log_img_back);
        img_delete= (ImageView) findViewById(R.id.detailed_communicate_log_img_delete);
        img_update= (ImageView) findViewById(R.id.detailed_communicate_log_img_update);
        a=this;
        communicate= GetCommunicateLogData.get();
        txt_theme.setText(communicate.getCommunication_theme());
        communicate_id=communicate.getCommunication_id();
        txt_content.setText(communicate.getCommunication_content());
        txt_next_communicate_content.setText(communicate.getNext_communicate_content());
        if (communicate.getNext_communicate_date()!=null) {
            boolean b = communicate.getNext_communicate_date().matches("\\d{4}-\\d{2}-\\d{2}");
            if (b) {
                txt_next_communicate_date.setText(communicate.getNext_communicate_date());
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd ");
                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                String time2 = communicate.getNext_communicate_date();
                long l = Long.parseLong(time2);
                Date date = new Date(l);
                String next_time = sdf.format(date);
                txt_next_communicate_date.setText(next_time);
            }
        }
        txt_create_name.setText(communicate.getCreate_name());
        SimpleDateFormat sdf1 = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
        sdf1.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String time1 = communicate.getCreate_time();
        long l1 = Long.parseLong(time1);
        Date date1 = new Date(l1);
        String create_time = sdf1.format(date1);
        txt_create_time.setText(create_time);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                builder = new AlertDialog.Builder(new ContextThemeWrapper(DetailedCommunicateLogMessage.this, R.style.custom_dialog));
                builder.setTitle("您确定要删除吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String result= NetWorkUtils.getCount(baseClass.delete_communicate_log_url+communicate_id,null);
                                try {
                                    JSONObject object=new JSONObject(result);
                                    String boolen=object.getString("boolean");
                                    if ("success".equals(boolen)){
                                        finish();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    }
                });
               builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialogInterface, int i) {

                   }
               });
                builder.show();
            }
        });
        img_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(DetailedCommunicateLogMessage.this,AddContactLogActivity.class);
                intent.putExtra("tag","communicate_update");
                intent.putExtra("KGA01",communicate_id);
                startActivity(intent);
            }
        });

    }


}
